/**
 * This file is a part of qloudgen-rul. 
 * You can redistribute qloudgen-rul and/or modify it under the terms of the Lesser GNU General Public License version 3. 
 * qloudgen-rul is distributed WITHOUT ANY WARRANTY. 
 *
 * See the Lesser GNU General Public License for more details at http://www.gnu.org/licenses/. 
 **/
package org.qloudgen.rul.guvnor.builder;

import java.util.Properties;
import java.io.InputStream;
import java.io.IOException;

import javax.enterprise.context.ApplicationScoped;

import org.guvnor.common.services.project.builder.model.BuildResults;
import org.guvnor.common.services.project.builder.service.PostBuildHandler;
import org.guvnor.common.services.project.model.GAV;

import org.qloudgen.rul.curator.RepositoryPathCacheSender;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author mike
 */
@ApplicationScoped
public class ZookeeperHandler implements PostBuildHandler {
	/**
	 *
	 */
	private static final Logger logger = LoggerFactory.getLogger( ZookeeperHandler.class );
	/**
	 *
	 */
	private RepositoryPathCacheSender sender = null;
	
	public ZookeeperHandler() {	  	  
		if (sender == null){
			sender = new RepositoryPathCacheSender();
			sender.init();
		}
		//
		logger.debug("zookeeper config:"+sender.toString());
	}
	
	public void process(BuildResults buildResults) {
		//
		GAV gav =  buildResults.getGAV();
		//
		String groupId = gav.getGroupId();
		String artifactId = gav.getArtifactId();
		String version = gav.getVersion();
		//
		if ( sender.isExist( sender.getPathFromReleaseId( groupId , artifactId , version ) ) ) {
			sender.updateChild( sender.getPathFromReleaseId( groupId , artifactId , version ) , ( new String( "update" ) ).getBytes() );
		}
		else {
			sender.addChild( sender.getPathFromReleaseId( groupId , artifactId , version ) , ( new String( "add" ) ).getBytes() );
		}
	}
}